package com.fmi.timedealer.plan;

import java.util.ArrayList;

import com.fmi.timedealer.ActivityItem;
import com.fmi.timedealer.R;
import com.fmi.timedealer.R.id;
import com.fmi.timedealer.R.layout;
import com.fmi.timedealer.datamanager.IDataManager;
import com.fmi.timedealer.datamanager.SQLiteDataManager;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;

public class ManagePlannedActivity extends Activity {
	
	public final static String ACTIVITY_BUNDLE_KEY = "ActivityObject";
	
	private PlannedActivity currentPlanedActivity = null;
	private IDataManager dbManager = null;
	ArrayList<ActivityItem> activitiesList = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.manage_planned_activity);
		
		dbManager = new SQLiteDataManager("MyDB", this);
		
		Bundle extras = getIntent().getExtras();
		currentPlanedActivity = (PlannedActivity)extras.getSerializable(ACTIVITY_BUNDLE_KEY);
		
		activitiesList = dbManager.getAllActivities();
		
		Spinner s = (Spinner) findViewById(R.id.activities_snipper);
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, getNamesForActivities(activitiesList));
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		s.setAdapter(adapter);
		
		Button btnSave = (Button)findViewById(R.id.btn_save_planned_activity);
		btnSave.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				updateCurrentValues();
				if (currentPlanedActivity.getId() == 0) {
					dbManager.insertPlannedActivity(currentPlanedActivity);
				}
				else {
					dbManager.updatePlannedActivity(currentPlanedActivity);
				}
				returnResult(Activity.RESULT_OK);
			}
		});
		
		Button btnCancel = (Button)findViewById(R.id.btn_cancel_planned_activity);
		btnCancel.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				returnResult(Activity.RESULT_CANCELED);
			}
		});
		
		fillViewsData(currentPlanedActivity);
	}
	
	private void updateCurrentValues() {
		
		Spinner s = (Spinner) findViewById(R.id.activities_snipper);
		if (s != null) {
			int position = s.getSelectedItemPosition();
			currentPlanedActivity.setActivityItem(activitiesList.get(position));
		}
		
		// Fill Duration data
		TextView tvDuration = (TextView)findViewById(R.id.text_planned_duration);
		if (tvDuration != null) {
			float duration = Float.parseFloat(tvDuration.getText().toString());
			currentPlanedActivity.setDuration(duration);
		}
	}
	
	private void returnResult(int resultCode) {

		if (resultCode == RESULT_OK) {
			getIntent().removeExtra(ACTIVITY_BUNDLE_KEY);
			getIntent().putExtra(ACTIVITY_BUNDLE_KEY, currentPlanedActivity);
		}
		this.setResult(resultCode, getIntent());
		this.finish();
	}
	
	private String[] getNamesForActivities(ArrayList<ActivityItem> activities) {
		String[] ids = new String[activities.size()];
		for (int i = 0; i < activities.size(); i++) {
			ids[i] = activities.get(i).getName();
		}
		return ids;
	}
	
	@SuppressWarnings("rawtypes")
	private void fillViewsData(PlannedActivity plannedActivity) {
		if (plannedActivity.getId() != 0) {
			TextView tvDuration = (TextView)findViewById(R.id.text_planned_duration);
			tvDuration.setText(String.valueOf(plannedActivity.getDuration()));
			
			Spinner s = (Spinner) findViewById(R.id.activities_snipper);
			ArrayAdapter actionAdapter = (ArrayAdapter) s.getAdapter();
			int actionPos = -1;
			for (int i = 0; i < actionAdapter.getCount(); i++) {
				String currItem = (String) actionAdapter.getItem(i);
				if (plannedActivity.getActivityItem().getName().equals(currItem)) {
					actionPos = i;
					break;
				}
			}
			s.setSelection(actionPos);
			s.setEnabled(false);
		}
	}
}
